package me.zhouzhuo810.blehelperdemo.ui.act;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.callback.BleWriteCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import me.zhouzhuo810.blehelperdemo.R;
import me.zhouzhuo810.blehelperdemo.ui.adapter.BleDeviceAdapter;
import me.zhouzhuo810.magpie.ui.act.BaseActivity;
import me.zhouzhuo810.magpie.ui.widget.MarkView;
import me.zhouzhuo810.magpie.ui.widget.TitleBar;
import me.zhouzhuo810.magpie.utils.ToastUtil;

/**
 * https://github.com.cnpmjs.org/zhouzhuo810
 */
public class BLEListActivity extends BaseActivity {

    private SmartRefreshLayout refreshLayout;
    private RecyclerView rv;
    private BleDeviceAdapter adapter;
    private TitleBar titleBar;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ble;
    }

    @Override
    public boolean shouldSupportMultiLanguage() {
        return false;
    }

    @Override
    public void initView(@Nullable Bundle bundle) {
        titleBar = findViewById(R.id.title_bar);
        refreshLayout = findViewById(R.id.refresh_layout);
        rv = findViewById(R.id.rv);
    }

    @Override
    public void initData() {
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BleDeviceAdapter(this, data);
        rv.setAdapter(adapter);

    }

    @Override
    public void initEvent() {
        titleBar.setOnTitleClickListener(new TitleBar.OnTitleClick() {
            @Override
            public void onLeftClick(ImageView imageView, MarkView markView, TextView textView) {
                closeAct();
            }

            @Override
            public void onTitleClick(TextView textView) {

            }

            @Override
            public void onRightClick(ImageView imageView, MarkView markView, TextView textView) {

            }
        });

        adapter.setOnDeviceClickListener(new BleDeviceAdapter.OnDeviceClickListener() {
            @Override
            public void onConnect(BleDevice bleDevice) {
                if (!BleManager.getInstance().isConnected(bleDevice)) {
                    BleManager.getInstance().cancelScan();
                    connect(bleDevice);
                }
            }

            @Override
            public void onDisConnect(final BleDevice bleDevice) {
                if (BleManager.getInstance().isConnected(bleDevice)) {
                    BleManager.getInstance().disconnect(bleDevice);
                }
            }

            @Override
            public void onDetail(BleDevice bleDevice) {
                if (BleManager.getInstance().isConnected(bleDevice)) {
//                    Intent intent = new Intent(BLEListActivity.this, OperationActivity.class);
//                    intent.putExtra(OperationActivity.KEY_DATA, bleDevice);
//                    startActivity(intent);
                }
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                startScan();
            }
        });
        refreshLayout.autoRefresh();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void write(BleDevice bleDevice) {
        UUID uuid = null;
        BluetoothGatt gatt = null;
        BluetoothGattService service = gatt.getService(uuid);
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid);

        String uuid_write = characteristic.getUuid().toString();
        BleManager.getInstance().write(bleDevice, uuid_write, uuid.toString(), null, null);
    }

    private void connect(final BleDevice bleDevice) {
        BleManager.getInstance().connect(bleDevice, new BleGattCallback() {
            @Override
            public void onStartConnect() {
                Log.e(TAG, "onStartConnect: ");
                showLoadingDialog("连接中...");
            }

            @Override
            public void onConnectFail(BleDevice bleDevice, BleException exception) {
                Log.e(TAG, "onConnectFail: " + exception.getDescription());
                hideLoadingDialog();
                ToastUtil.showShortToast("连接失败");
            }

            @Override
            public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
                Log.e(TAG, "onConnectSuccess: ");
                hideLoadingDialog();
//                adapter.addDevice(bleDevice);
                data.add(bleDevice);
                adapter.notifyDataSetChanged();

            }


            @Override
            public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, BluetoothGatt gatt, int status) {
                hideLoadingDialog();

                // adapter.removeDevice(bleDevice);
                adapter.notifyDataSetChanged();

                if (isActiveDisConnected) {
                    ToastUtil.showShortToast("已断开连接");
                } else {
                    ToastUtil.showShortToast("已断开连接");
//                    ObserverManager.getInstance().notifyObserver(bleDevice);
                }

            }
        });
    }

    public static final String TAG = BLEListActivity.class.getSimpleName();
    List<BleDevice> data = new ArrayList<>();

    private void read() {

//        onCharacteristicChanged
    }

    private void write(BleDevice bleDevice,
                       String uuid_service,
                       String uuid_write) {

        byte[] data = null;

        BleWriteCallback callback = new BleWriteCallback() {
            @Override
            public void onWriteSuccess(int current, int total, byte[] justWrite) {
                Log.e(TAG, "onWriteSuccess: ");
            }

            @Override
            public void onWriteFailure(BleException exception) {

            }
        };
        BleManager.getInstance().write(bleDevice, uuid_service, uuid_write, data, callback);
    }

    private void startScan() {
        BleManager.getInstance().scan(new BleScanCallback() {
            @Override
            public void onScanStarted(boolean success) {
                Log.e(TAG, "onScanStarted: ");
                data.clear();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onLeScan(BleDevice bleDevice) {
                super.onLeScan(bleDevice);
            }

            @Override
            public void onScanning(BleDevice bleDevice) {
                Log.e(TAG, "onScanning:" + bleDevice.getMac());
//                adapter.addDevice(bleDevice);
                data.add(bleDevice);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onScanFinished(List<BleDevice> scanResultList) {
                for (BleDevice bleDevice : scanResultList) {
                    Log.e(TAG, "onScanFinished: " + bleDevice.getName());
                }
                adapter.updateAll(scanResultList);
                adapter.notifyDataSetChanged();
                refreshLayout.finishRefresh();
            }
        });
    }
}
